<?php
/**
 * Creater: 李青林
 * Date Time : 2018/8/14 11:15
 */

namespace App\Http\Controllers;

use App\Http\Models\UserCapital;
use App\Http\Models\RechargeLog;
use App\Http\Models\VoucherLog;
use App\Http\Models\User;
use App\Http\Models\UserIntegralLog;
use Auth;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Http\Models\UserSubmitAudit;
use App\Http\Models\StatisticsUserVoucher;
use Monolog\Handler\IFTTTHandler;

class UsercapitalController extends Controller
{
    /**
     * 获取用户的点券消费记录
     * @param Request $request
     * @return string
     */
    public function getVoucherLogList(Request $request)
    {
        $userId = Auth::id();
        $list = VoucherLog::leftjoin('goods', 'users_voucher_log.goods_id', '=', 'goods.id')
            ->select('users_voucher_log.id', 'users_voucher_log.created_at', 'users_voucher_log.status', 'users_voucher_log.user_id', 'users_voucher_log.status', 'users_voucher_log.content', 'users_voucher_log.number', 'goods.goodstitle')
            ->where('user_id', $userId)
            ->orderBy('users_voucher_log.id', 'desc')
            ->paginate(env('LIMIT'));
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    public function getUserIntegralInfo()
    {
        $data = UserCapital::select('voucher_number', 'integral')->where(['user_id' => Auth::id()])->first();
        $user = User::select('name', 'avatar', 'signature')->where(['id' => Auth::id()])->first()->toArray();
        if ($data) {
            $data = array_merge($user, $data->toArray());
            return response()->json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
        } else {
            return response()->json(['code' => 1, 'msg' => '暂无积分信息', 'data' => '']);
        }
    }

    /**
     * 获取用户的充值记录
     * @param Request $request
     * @return string
     */
    public function getRechargeLog(Request $request)
    {
        $userId = Auth::id();
        $list = RechargeLog::where(['user_id' => $userId])->paginate(env('LIMIT'));
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 获用户点券信息
     * @param Request $request
     * @return string
     */
    public function getUserVoucher(Request $request)
    {
        $userId = Auth::id();
        $uswerCapital = UserCapital::where('user_id', $userId)->first();
        $numer = $uswerCapital ? $uswerCapital->voucher_number : 0;
        /*  $voucher = StatisticsUserVoucher::where(['user_id' => $userId])->first();
         $dayNumer = $voucher ? $voucher->day_voucher : 0;
         $mouthNumber = $voucher ? $voucher->months_voucher : 0;*/
        //当天消费
        $release_number = VoucherLog::where(['user_id' => $userId, 'status' => 0])
            ->whereDate('created_at', date('Ymd'))
            ->sum('number');
        $refuse_number = VoucherLog::where(['user_id' => $userId, 'status' => 1])
            ->leftjoin('goods', 'users_voucher_log.goods_id', '=', 'goods.id')
            ->whereDate('goods.created_at', date('Ymd'))
            ->whereMonth('users_voucher_log.created_at', date('m'))
            ->sum('number');
        //当月消费
        $release_month_number = VoucherLog::where(['user_id' => $userId, 'status' => 0])
            ->whereMonth('created_at', date('m'))
            ->sum('number');
        $refuse_month_number = VoucherLog::where(['user_id' => $userId, 'status' => 1])
            ->leftjoin('goods', 'users_voucher_log.goods_id', '=', 'goods.id')
            ->whereMonth('goods.created_at', date('m'))
            ->whereMonth('users_voucher_log.created_at', date('m'))
            ->sum('number');
        $dayNumer = $release_number - $refuse_number;
        $mouthNumber = $release_month_number - $refuse_month_number;
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => ['userVoucherNumber' => (int)$numer, 'dayNumer' => (int)$dayNumer, 'mouthNumber' => (int)$mouthNumber]]);

    }

    /**
     * 检测用户是否具有放单权限
     */
    public function getUserIsSend(Request $request)
    {
        $u_status = -1;
        $error_info = [
            '1' => '放单权限审核中', // 1
            '2' => '放单权限资格审核通过',// 2
            '3' => '放单权限审核被拒', // 3
            '4' => '积分用完停用',// 4
            '5' => '放单权限资格审核通过,可以放单!',//5
            '6' => '暂未开通放单权限',// 6
            '7' => '点券使用完毕,暂不可放单' // 7
        ];
        $uid = Auth::id();//获取当前登陆用户ID
        if (empty($uid)) {
            return json_encode(['code' => 1, 'msg' => '用户尚未登陆！']);
        }
        $userAudit = UserSubmitAudit::where(['uid' => $uid])->first();
        if (empty($userAudit)) {
            $u_status = 6;
        } elseif ($userAudit->state == 0) {
            $u_status = 1;
        } elseif ($userAudit->state == 1) {
            $u_status = 2;
        } elseif ($userAudit->state == 2) {
            $u_status = 3;
        }
        $usercapital = UserCapital::where('user_id', $uid)->first();
        if ($u_status == 2 && !empty($usercapital)) {
            if (!($usercapital->integral > 0)) {
                $u_status = 4;
            } elseif (!($usercapital->voucher_number > 0)) {
                $u_status = 7;
            } else {
                $u_status = 5;
            }
        }
        if ($u_status == 5) {
            return json_encode(['code' => 0, 'msg' => $error_info[$u_status], 'submit_audit' => $userAudit]);
        } else {
            return json_encode(['code' => $u_status, 'msg' => $error_info[$u_status], 'submit_audit' => $userAudit]);
        }
    }


}